from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from model import Teacher, Course

# 1. 链接数据库等准备工作
engine = create_engine('mysql+pymysql://root:python@localhost:3306/19python')
DBSession = sessionmaker(bind=engine)
session = DBSession()

# 2. 添加数据数据
# 创建老师对象
teacher1 = Teacher(name='老师1')
teacher2 = Teacher(name='老师2')
teacher3 = Teacher(name='老师3')
# 创建课程对象 注意传入对应的老师
course01 = Course(name='数学', teacher=teacher1)
course02 = Course(name='英语', teacher=teacher2)
course03 = Course(name='语文', teacher=teacher3)
# 添加数据
session.add_all([teacher1, teacher2, teacher3, course01, course02, course03])

# 3. 提交（只要是增删改任意一种都意味着对数据进行了修改，都需要commit）
session.commit()

# 4. 关闭session
session.close()
